package cn.edu.zjicm.wordsnet_d.activity;

import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;

public class DefaultWallpaperSelectActivity extends WordBaseActivity {
	GridView gridView;
	int[] picture = { R.drawable.lock_screen_bg0, R.drawable.lock_screen_bg1,
			R.drawable.lock_screen_bg2, R.drawable.lock_screen_bg3,
			R.drawable.lock_screen_bg5, R.drawable.lock_screen_bg6, 
			R.drawable.lock_screen_bg7, R.drawable.lock_screen_bg8,
			R.drawable.lock_screen_bg10,R.drawable.lock_screen_bg11,
			R.drawable.lock_screen_bg12};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("推荐壁纸");
		setContentView(R.layout.activity_default_wallpaper_select);
		gridView = (GridView) findViewById(R.id.default_wallpaper_select_gridView);
		gridView.setAdapter(new MyAdapter(this));
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				AppPreference.setLockScreenWallpaper(DefaultWallpaperSelectActivity.this, "default");
				AppPreference.setDefaultWallpaperImg(
						DefaultWallpaperSelectActivity.this, picture[arg2]);
				setResult(Activity.RESULT_OK);
				finish();
			}
		});
	}

	class MyAdapter extends BaseAdapter{
		Context context;
		
		public MyAdapter(Context context){
			this.context = context;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return picture.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return picture[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder holder = null;
			if(convertView==null){
				convertView = LayoutInflater.from(context).inflate(R.layout.default_wallpaper_select_item, null);
				holder = new Holder();
				
			}else{
				holder = (Holder) convertView.getTag();
			}
			holder.img = (ImageView) convertView.findViewById(R.id.default_wallpaper_img);
			holder.img.setImageResource(picture[position]);
			convertView.setTag(holder);
			return convertView;
		}
		private class Holder{
			ImageView img = null;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
